package websql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;

public class DBcon {
	private static String drv = "org.gjt.mm.mysql.Driver";
	private static String url = "jdbc:mysql://localhost/websql?useUnicode=true&characterEncoding=utf-8";
	private static String usr = "root";
	private static String psd = "";
	private static Connection conn;

	public static boolean testdb() {
		boolean result = false;
		// initDBinfo();
		try {
			Class.forName(drv).newInstance();
			Connection conn = DriverManager.getConnection(url, usr, psd);
			Statement stm = conn.createStatement();
			ResultSet rs = stm.executeQuery("SELECT * FROM charge");
			ResultSetMetaData rsmd = rs.getMetaData();
			int numberOfColumns = rsmd.getColumnCount();
			while (rs.next()) {
				for (int i = 1; i <= numberOfColumns; i++) {
					if (i > 1) {
						System.out.print(", ");
					}
					String columnValue = rs.getString(i);
					System.out.print(columnValue);
				}
				System.out.println("");
			}
			stm.close();
			conn.close();
			result = true;
		} catch (Exception e) {
			System.out.println(e);
			result = false;
		}
		return result;
	}

	public static Connection getConn() throws Exception {
		// initDBinfo();
		Class.forName(drv).newInstance();
		conn = DriverManager.getConnection(url, usr, psd);
		return conn;

	}
	public static String exsql(String sql) throws Exception {
		Class.forName(drv).newInstance();
		conn = DriverManager.getConnection(url, usr, psd);
		Statement stm=conn.createStatement();
		stm.execute(sql);
		return sql;
	}

	public static void main(String argv[]) {
		System.out.println(DBcon.testdb());
	}

}
